package com.kcnet.qk.util;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.service.AutoAddPeopleService;

import java.io.File;
import java.io.FileInputStream;

import cn.jesse.nativelogger.NLogger;


/**
 * Created by czc on 2017/7/3.
 */

public class AppUtil {

    /**
     * 判断软件是否激活
     * @return
     */
    public static boolean isValiditySoftWare(){
        String account = PreferenceHelper.getString(Const.PREF_KEY_ACCOUNT,"");
        String accountMd5 = PreferenceHelper.getString(Const.PREF_KEY_ACCOUNT_MD5,"");
        boolean validity = PreferenceHelper.getBoolean(Const.PREF_KEY_ACTIVATE,false);
        if (validity && TextUtils.equals(accountMd5,Md5.encode(account))){
            return true;
        }else {
            return false;
        }
    }

    /**
     * check has permission
     * @return
     */
    public static  boolean checkPermission(Context context) {
        int ok = 0;
        try {
            ok = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
        } catch (Settings.SettingNotFoundException e) {
        }

        TextUtils.SimpleStringSplitter ms = new TextUtils.SimpleStringSplitter(':');
        if (ok == 1) {
            String settingValue = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
            if (settingValue != null) {
                ms.setString(settingValue);
                while (ms.hasNext()) {
                    String accessibilityService = ms.next();
                    if (accessibilityService.contains(AutoAddPeopleService.class.getSimpleName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    public static  void jumpToFb(Context context) {

        try {

            NLogger.d("AppUtil","启动FB");
            RootShell.execShellCmd("am force-stop com.facebook.katana");

            Thread.sleep(2000);
            Intent intent;
            PackageManager packageManager = context.getPackageManager();
            intent = packageManager.getLaunchIntentForPackage("com.facebook.katana");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
         //  RootShell.execShellCmd("am start -n com.facebook.katana/.app.FacebookSplashScreenActivity");
            NLogger.d("AppUtil","启动FB成功");

        } catch (Exception e) {
            ToastUtil.showToast(context,"亲，你安装了FaceBook了吗？");
            e.printStackTrace();
        }
    }

    public static  void jumpToMsg(Context context) {

        try {
            ActivityManager m= (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
            m.killBackgroundProcesses("com.facebook.orca");
            Intent intent;
            PackageManager packageManager = context.getPackageManager();
            intent = packageManager.getLaunchIntentForPackage("com.facebook.orca");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
        } catch (Exception e) {
            ToastUtil.showToast(context,"亲，你安装了Messager了吗？");
            e.printStackTrace();
        }
    }


    public static boolean isNewUser(){
        return PreferenceHelper.getBoolean(Const.PREF_IS_NEW_USER, true);
    }

    public static void jumpSetting(Context context) {
        // jump to setting permission
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        context.startActivity(intent);
    }

    /**
     * 获取应用版本号
     */
    public static int getVersionCode(final Context con) {
        int version = 1;
        PackageManager packageManager = con.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    con.getPackageName(), 0);
            version = packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return version;
    }

    /**
     * 获取应用版本名称
     *
     * @param context
     * @return
     */
    public static String getVersionName(final Context context) {
        String versionName = "1.0.0";
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionName;
    }




    public static  String loadIPConf() {
        String path = "/Applications/ip.txt";
        String result=null;
        try {
            File f=new File(Environment.getExternalStorageDirectory().getPath()+path);
            int length=(int)f.length();
            byte[] buff=new byte[length];
            FileInputStream fin=new FileInputStream(f);
            fin.read(buff);
            fin.close();
            result=new String(buff,"UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

}
